package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * Author chongge
 * Time 2019/10/26 18:34
 * ClassName com.itheima.controller
 */
@RestController
@RequestMapping(value = "/setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    /***
     * 图片上传七牛云
     * @param imgFile
     * @return
     */
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public Result upload(@RequestParam("imgFile")MultipartFile imgFile){
        //原始文件名
        String originalFilename = imgFile.getOriginalFilename();
        //后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //唯一文件名
        String filename = UUID.randomUUID().toString()+suffix;
        //调用七牛云上传存储空间
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),filename);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,filename);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,filename);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL,filename);
        }
    }


    /***
     * 增加套餐
     * @param setmeal
     * @param ids
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody Setmeal setmeal, Integer[] ids){
        try {
            setmealService.add(setmeal,ids);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }


    /***
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping(value = "/findPage",method = RequestMethod.POST)
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;
    }

    /***
     * 回显数据之根据Id查找套餐信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public Result findById(Integer id){

        Setmeal setmeal = setmealService.findById(id);
        if (setmeal != null) {
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,setmeal);
        }

        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);

    }


    /***
     * 回显数据之根据套餐的id查出对应的检查组id
     * @return
     */
    @RequestMapping(value = "/findCheckgroupIdsBySetmealId",method = RequestMethod.GET)
    public List<Integer> findCheckgroupIdsBySetmealId(Integer id){
        List<Integer> list = setmealService.findCheckgroupIdsBySetmealId(id);
        return list;
    }


    /***
     * 编辑套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Result edit(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try {
            setmealService.edit(setmeal,checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }

        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /***
     * 删除检查组
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Result delete(Integer id){
        try {
            setmealService.delete(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

}
